﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmLogin : Form
    {
        public static Sunisoft.IrisSkin.SkinEngine se = null;
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtPwd.Text = "";
            string sql = "select dname from dietitianinfo";
            DataSet ds = DbHelperOleDb.Query(sql);
            DataRow dr= ds.Tables[0].NewRow();
            dr[0] = "admin";
            ds.Tables[0].Rows.InsertAt(dr, 0);
            cmbType.DataSource = ds.Tables[0];
            cmbType.DisplayMember = "dname";            
            cmbType.SelectedIndex = 0;
            #region 注册代码 使用配置文件保存内容
            Computer com = new Computer();
            if (File.Exists("code.txt"))
            {
                FileStream f = File.OpenRead("code.txt");
                StreamReader sw = new StreamReader(f, System.Text.Encoding.Default);
                if (!sw.EndOfStream)
                {
                    string t = sw.ReadLine();
                    sw.Close();
                    f.Close();
                    string c = "jinan123";
                    string suiji = Computer.EncryptDES(com.GetDiskID().ToString(), c);
                    if (suiji != t.Trim().ToString())
                    {
                        new reg().ShowDialog();
                    }
                }
                else
                {
                    sw.Close();
                    f.Close();
                    new reg().ShowDialog();
                }
            }
            else
            {
                new reg().ShowDialog();
            }
                
            #endregion

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {            
           //登录功能已测试完成
            string name = cmbType.Text;
            string pwd = txtPwd.Text.Trim();
            string sql = "";
            bool isadmin = false;
            if (name == "admin")//如果是俱乐部管理员，从俱乐部表里找信息
            {
                sql = string.Format("select sid,sadmin,stel from shopinfo where sadmin='{0}' and spwd='{1}'", name, pwd);
                isadmin = true;
            }
            else//如果不是俱乐部管理员（是营养师），从营养师表中找信息
            {
                sql = string.Format("select did,dname,dtel from dietitianinfo where dname='{0}' and dpwd='{1}'", name, pwd);
            }
            DataSet ds = DbHelperOleDb.Query(sql);
            if (ds.Tables[0].Rows.Count == 1)
            {
                userinfo.id = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                userinfo.name = ds.Tables[0].Rows[0][1].ToString();
                userinfo.tel = ds.Tables[0].Rows[0][2].ToString();
                userinfo.isadmin = isadmin;
                sql = "select sname,saddress from shopinfo";
                ds=DbHelperOleDb.Query(sql);
                shopinfo.name = ds.Tables[0].Rows[0][0].ToString();
                shopinfo.add = ds.Tables[0].Rows[0][1].ToString();
                frmadmin f = new frmadmin();
                this.Hide();
                myform.frmlogin = this;
                f.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！");
            } 
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLogin_Paint(object sender, PaintEventArgs e)
        {
            //txtPwd.Text = "";
        }

        private void frmLogin_Activated(object sender, EventArgs e)
        {
            txtPwd.Text = "";
        }
    }
}
